package kr.ac.kaist.idb.csnql.clause;

import java.io.Serializable;

/**
 * @brief SAMPLE PERIOD-FOR clause
 * @author Jongheon Park (korex527@kaist.ac.kr)
 */
public class SNQLSample implements Serializable{
	
	/**
	 * @brief Serial version UID which is automatically generated
	 */
	private static final long serialVersionUID = 6850504439623914666L;

	/** @brief SAMPLE PERIOD - FOR type */
	public final static int SAMPLE_PERIOD_TYPE = 1;
	
	/** @brief ONCE type */
	public final static int ONCE_TYPE = 2;
	
	/** @brief Type of this clause */
	private int sample_type;
	
	/** @brief Sample period (PERIOD) value */
	private String period;
	
	/** @brief Sample duration (FOR) value */
	private String duration;
	
	/** @brief Existence of FOR time  */
	private Boolean hasFor = false;
	
	/**
	 * @brief Instantiates a new SNQLSample with type
	 */
	public SNQLSample()
	{
		sample_type = 0;
		period = new String();
		duration = new String();
		hasFor = false;
	}

	/**
	 * @brief Instantiates a new SNQLSample with type
	 *
	 * @param type The type
	 */
	public SNQLSample(int type)
	{
		sample_type = type;
		period = new String();
		duration = new String();
		hasFor = false;
	}
	
	/**
	 * @return the period time
	 * @brief Get the period time
	 */
	public long getPeriodTimeToLong()
	{
		return getTimeToLong(period);
	}
	
	/**
	 * @return the duration time
	 * @brief Get the duration time
	 */
	public long getDurationTimeToLong()
	{
		return getTimeToLong(duration);
	}
	
	/**
	 * @return the sample_type
	 * @brief Get the sample_type
	 */
	public int getSample_type() {
		return sample_type;
	}

	/**
	 * @param sample_type SAMPLE clause type
	 * @brief Set the SAMPLE clause type
	 */
	public void setSample_type(int sample_type) {
		this.sample_type = sample_type;
	}

	/**
	 * @return the period time
	 * @brief Get the period time
	 */
	public String getPeriod() {
		return period;
	}

	/**
	 * @param time_ New period time
	 * @brief Set the period time
	 */
	public void setPeriod(String time_) {
		this.period = time_;
	}

	/**
	 * @return true, if it has FOR clause
	 * @brief Check if it has FOR clause
	 */
	public Boolean getHasFor() {
		return hasFor;
	}

	/**
	 * @param isFor_ The existence of FOR clause
	 * @brief Set the existence of FOR clause
	 */
	public void setHasFor(Boolean isFor_) {
		this.hasFor = isFor_;
	}
	
	/**
	 * @return the duration time
	 * @brief Get the duration time
	 */
	public String getDuration() {
		return duration;
	}

	/**
	 * @param for_time_ The duration time
	 * @brief Set the duration time
	 */
	public void setDuration(String for_time_) {
		this.hasFor = true;
		this.duration = for_time_;
	}

	/**
	 * Gets the time to long.
	 *
	 * @param timeVar The time variable string
	 * @return time value (long type)
	 * @brief Get the time value (long type)
	 */
	private long getTimeToLong(String timeVar)
	{
		long mTime;
		char time_unit = 0;
		int time = 0;
		
		if(timeVar.length() != 0)
		{
			time_unit = timeVar.charAt(timeVar.length()-1);
			time = Integer.parseInt(timeVar.substring(0, timeVar.length()-1));
		}
		else
		{
			// ??
			System.out.println(timeVar);
		}
		
		//["s","m","h","D","M","Y"] 
		if( time_unit == 's')
		{
			mTime = time;
			return mTime* 1000;
		}
		else if( time_unit == 'm')
		{
			mTime = 60*time;
			return mTime* 1000;
		}
		else if (time_unit == 'h')
		{	
			mTime = 3600*time;
			return mTime* 1000;
		}
		else if (time_unit == 'D')
		{
			mTime = 24*3600*time;
			return mTime* 1000;
		}
		else if (time_unit == 'M')
		{
			mTime = 30*24*3600*time;
			return mTime* 1000;
		}
		else if (time_unit == 'Y')
		{
			mTime = 365*24*3600*time;
			return mTime * 1000;
		}
			
		return -1;
	}
	
	public String toString()
	{
		StringBuffer stringbuffer = new StringBuffer();
		switch(sample_type)
		{
		case SAMPLE_PERIOD_TYPE:
			stringbuffer.append("SAMPLE PERIOD");
			stringbuffer.append(" " + period);
			if (duration != "")
			{
				stringbuffer.append(" FOR");
				stringbuffer.append(" " + duration);
			}
			break;
		case ONCE_TYPE:
			stringbuffer.append("ONCE");
			break;
		default :
			break;
		}
		
		return stringbuffer.toString();
	}
	
	public Boolean getIsFor_() {
		return hasFor;
	}

	public void setIsFor_(Boolean isFor_) {		
		this.hasFor = isFor_;
	}
}
